# Tested on MacOS Ventura 13.2.1
# Python 3.11.4
# * pip3 install geopandas==0.14.0
# * pip3 install pyshp==2.3.1
# * pip3 install shapley==1.8.4 (critical)
# * pip3 install numpy==1.24.2
# * pip3 install plotly==5.18.0
# * pip3 install plotly-geo==1.0.0
# * pip3 install pandas==1.5.3

from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly

import numpy as np
import pandas as pd

"""Read in your data. In my case, the data is csv file that looks like 
 county, infc, outdegree, eigenvector, ...
 1001, 65, 32, 154,...
 1002, 78, 39, 0.4, ...
 ....
"""
df_sample = pd.read_csv('centrality.csv') # change path to the centrality.csv generated by 0-CEN.m
df_sample['FIPS'] = df_sample['county']

# Choose your color gradients, you can google heatmap colorscale for other choices
colorscale = ["#f7fbff","#deebf7","#c6dbef","#9ecae1", "#6baed6","#4292c6",
              "#2171b5", "#08519c","#08306b"]

#Create n-1 endpoints, where n is the number of color gradients 
#Since I will using rank variables to plot, I simply create endpts from 1 to n-1; 
#Alternatively, you can provide your own endpts such as endpts = [30, 50, 70, ...] 
endpts = list(np.linspace(1, 8, len(colorscale) - 1))
print (endpts)


# Create ranks of eigenvector for example; 
df_sample['eigenvector_rk_green'] = pd.qcut(df_sample['eigenvector'], 
        9, labels = False)  

fips = df_sample['FIPS'].tolist()
values1 = df_sample['eigenvector_rk_green'].tolist()
fig1 = ff.create_choropleth(
    fips=fips, values=values1,
    colorscale = colorscale, 
    show_state_data=False,
    show_hover=True, centroid_marker={'opacity': 0},
    asp=2.9, 
    county_outline={'color': 'rgb(224, 224, 224)', 'width': 0.5},
    state_outline={'width': 1}    
    #legend_title='Connection Ranks'
)

fig1.layout.template = None
fig1.update_layout(showlegend=False)
fig1.show()
